﻿// $Id: ControlQuestion.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c DataActivities.UI.Forms.ControlQuestion.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Salamanca.DataActivities.UI.Forms
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Question implemented as a <see cref="Control" />.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class ControlQuestion:
        Question
    {

        /// <summary>Initializes a new <see cref="ControlQuestion" /> instance.</summary>
        public ControlQuestion(Control control)
        {
            _Control=control;
        }

        /// <summary>Asks the question.</summary>
        public override ActivityStateResult Ask(IActivityController controller)
        {
            AnswerController c=new AnswerController();

            Control cancel=c.GetCancelControl(_Control);
            if (cancel!=null)
            {
                _CancelEvent=new EventHandler(
                    delegate {
                        controller.Execute(ActivityStateResult.Cancel);
                    }
                );
                cancel.Click+=_CancelEvent;
            }

            Control end=c.GetEndControl(_Control);
            if (end!=null)
            {
                _EndEvent=new EventHandler(
                    delegate {
                        if (controller.CurrentState.Validate())
                            controller.Execute(ActivityStateResult.End);
                    }
                );
                end.Click+=_EndEvent;
            }

            Control next=c.GetNextControl(_Control);
            if (next!=null)
            {
                _NextEvent=new EventHandler(
                    delegate {
                        if (controller.CurrentState.Validate())
                            controller.Execute(ActivityStateResult.Next);
                    }
                );
                next.Click+=_NextEvent;
            }

            Control previous=c.GetPreviousControl(_Control);
            if (previous!=null)
            {
                _PreviousEvent=new EventHandler(
                    delegate {
                        controller.Execute(ActivityStateResult.Previous);
                    }
                );
                previous.Click+=_PreviousEvent;
            }

            _ValidatingEvent=new CancelEventHandler(
                delegate(object sender, CancelEventArgs e) {
                    Control ac=_Control.FindForm().ActiveControl;
                    e.Cancel|=(ac!=cancel) && (ac!=previous) && (!controller.CurrentState.Validate());
                }
            );
            _Control.Validating+=_ValidatingEvent;

            _Control.Disposed+=new EventHandler(
                delegate {
                    ReleaseControl();
                }
            );

            return ActivityStateResult.Stop;
        }

        /// <summary>Raised the <see cref="Question.Terminated" /> event.</summary>
        protected override void OnTerminated(QuestionEventArgs e)
        {
            ReleaseControl();

            base.OnTerminated(e);
        }

        private void ReleaseControl()
        {
            if (!_IsReleased)
            {
                AnswerController c=new AnswerController();

                Control cancel=c.GetCancelControl(_Control);
                if ((cancel!=null) && (_CancelEvent!=null))
                {
                    cancel.Click-=_CancelEvent;
                }

                Control end=c.GetEndControl(_Control);
                if ((end!=null) && (_EndEvent!=null))
                {
                    end.Click-=_EndEvent;
                }

                Control next=c.GetNextControl(_Control);
                if ((next!=null) && (_NextEvent!=null))
                {
                    next.Click-=_NextEvent;
                }

                Control previous=c.GetPreviousControl(_Control);
                if ((previous!=null) && (_PreviousEvent!=null))
                {
                    previous.Click-=_PreviousEvent;
                }

                _Control.Validating-=_ValidatingEvent;

                _IsReleased=true;
            }
        }

        /// <summary>Gets the name of the question.</summary>
        public string Name
        {
            get
            {
                QuestionProvider p=new QuestionProvider();
                return p.GetQuestionName(Control);
            }
        }

        /// <summary>Gets the control that implements the question.</summary>
        protected Control Control
        {
            get
            {
                return _Control;
            }
        }

        private EventHandler _CancelEvent;
        private EventHandler _EndEvent;
        private EventHandler _NextEvent;
        private EventHandler _PreviousEvent;
        private CancelEventHandler _ValidatingEvent;
        private bool _IsReleased;
        private Control _Control;

    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Question implemented as a <see cref="Control" />.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class ControlQuestion<TAnswer>:
        ControlQuestion
    {

        /// <summary>Initializes a new <see cref="ControlQuestion{TAnswer}" /> instance.</summary>
        public ControlQuestion(Control control):
            base(control)
        {
        }

        /// <summary>Raises the <see cref="Question.Initialized" /> event.</summary>
        protected override void OnInitialized(QuestionEventArgs e)
        {
            base.OnInitialized(e);

            IDataBag<TAnswer> control=Control as IDataBag<TAnswer>;
            IDataBag<TAnswer> data=e.Controller.CurrentState.Data as IDataBag<TAnswer>;
            if ((control!=null)&&(data!=null))
                control.Data=data.Data;
        }

        /// <summary>Raises the <see cref="Question.Answering" /> event.</summary>
        protected override void OnAnswering(AnswerEventArgs e)
        {
            base.OnAnswering(e);

            IAnswerContainer<TAnswer> control=Control as IAnswerContainer<TAnswer>;
            IDataBag<TAnswer> data=e.Data as IDataBag<TAnswer>;
            if (control!=null)
            {
                e.Answer=control.Answer;
                if (data!=null)
                    data.Data=control.Answer;
            }
        }
    }
}
